package com.lzy.shops.service.impl;

import com.lzy.shops.bean.Area;
import com.lzy.shops.mapper.AreaMapper;
import com.lzy.shops.service.AreaService;
import com.lzy.shops.utils.exception.ActionException;
import com.lzy.shops.utils.exception.ExistException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AreaServiceImpl implements AreaService {

    @Autowired
    private AreaMapper areaMapper;

    @Override
    public Area findProvince(Integer id) {
        Area province = areaMapper.findProvince(id);
        if (province != null) {
            List<Area> citys = this.findChildren(province.getId());//查询省下面的所有市
            digui(citys); //调用递归算法查询市以下的区县
            province.setChildren(citys);
        }
        return province;
    }

    @Override
    public List<Area> findChildren(Integer id) {
        return areaMapper.findChildren(id);
    }

    @Override
    public Integer addArea(Area area) throws ExistException {
        int currentId = 0;
        Integer sameName = areaMapper.findByIdSameName(area.getPid(), area.getName());
        if(sameName==0){
            if(area.getId()!=null && (area.getPid()!=null||area.getPid()!=0)){
                Integer areaMaxId = this.findAreaMaxId(area.getPid());
                currentId = areaMaxId + 100;
            }else{
                if(area.getPid()!=null&&area.getPid()!=0){
                    Integer id = area.getPid();
                    Integer areaMaxId = this.findAreaMaxId(id);
                    if(areaMaxId!=null){
                        currentId = areaMaxId+1;
                    }else{
                        currentId = ++id;
                        Area province = areaMapper.findProvince(currentId);// 查找当前id是否已存在
                        if(province!=null){
                            Integer max1 = areaMapper.findAreaMaxId(province.getPid());// 查找同一父类下的最大值
                            currentId = ++max1;
                        }
                    }
                }
            }
        }else{
            throw new ExistException("改地区名已存在");
        }
        area.setId(currentId);
        return areaMapper.addArea(area);
    }

    @Override
    public Integer findAreaMaxId(Integer area_id) {
        return areaMapper.findAreaMaxId(area_id);
    }

    @Override
    public Integer findByIdSameName(Integer Pid, String name) {
        return areaMapper.findByIdSameName(Pid,name);
    }

    @Override
    @Transactional
    public Map<String,String> deleteArea(Integer id) throws ActionException {
        List<Area> children = this.findChildren(id);
        Integer count = 0;
        Map<String,String> info = new HashMap<>();
        if(children.size()>=1){ // 存在子地区
            for (Area child : children) {
                Integer integer = areaMapper.deleteArea(child.getId());
                if(integer==1){
                    count++;
                }else{
                    throw new ActionException("删除失败");
                }
            }
            Integer integer = areaMapper.deleteArea(id);
            if(integer!=1){
                throw new ActionException("删除失败");
            }
            info.put("msg","成功删除一个父地区,其中包含了"+count+"个子地区");
        }else{
            Integer integer = areaMapper.deleteArea(id);
            if(integer!=1){
                throw new ActionException("删除失败");
            }
            info.put("msg","成功删除");
        }
        return info;
    }

    @Override
    public List<Area> findByName(String areaName) {
        List<Area> list = areaMapper.findByName(areaName);
        digui(list);
        return list;
    }

    private void digui(List<Area> citys) {
        List<Area> retList = new ArrayList<>();
        for (Area c : citys) {
            retList = areaMapper.findChildren(c.getId());
            if (retList.size() > 0) {
                c.setIsLeaf(true);
                c.setChildren(retList);
                if(retList.size()>=1){
                    digui(retList);
                }
            }
        }
    }
}
